package com.swipemaster.game.modes;

import android.app.Activity;

import com.google.analytics.tracking.android.EasyTracker;
import com.swipemaster.R;
import com.swipemaster.activities.GameActivity;
import com.swipemaster.common.Const;
import com.swipemaster.common.Global;
import com.swipemaster.common.Prefs;
import com.swipemaster.dialogs.Dialogs;
import com.swipemaster.game.GameSurface;
import com.swipemaster.levels.levelinfo.LevelInfo;
import com.swipemaster.network.task.IsWorldRecordTask;
import com.swipemaster.record.ChallengeRecord;
import com.swipemaster.record.IntegerRecord;
import com.swipemaster.record.Record;
import com.swipemaster.record.RecordId;
import com.swipemaster.record.RecordsManager;

public class ChallengeMode extends LevelsMode
{
	private double challengeTime;
	private static boolean isHelpHidden = Global.app.prefs.getBoolean(Prefs.CHALLENGE_HELP_KEY, false);

	public ChallengeMode()
	{
		super(new RecordId(RecordId.CHALLENGE));
	}

	@Override
	public void addModePlayed(boolean isWinner)
	{
		super.addModePlayed(isWinner);
		
		if (!isWinner)
		{
			// Increment games played counter only once, on the end of challenge.
			final IntegerRecord record = (IntegerRecord) RecordsManager.getRecord(RecordId.CHALLENGE_GAMES_PLAYED);
			record.counter++;
		}

		EasyTracker.getTracker().sendView("Challenge Game");
	}

	@Override
	public int getPlayedTimes()
	{
		final IntegerRecord record = (IntegerRecord) RecordsManager.getRecord(RecordId.CHALLENGE_GAMES_PLAYED);
		return record.counter;
	}

	@Override
	public Record getRecord()
	{
		final ChallengeRecord record = new ChallengeRecord();
		record.lastLevelIndex = levelIndex;
		record.time = challengeTime;
		return record;
	}

	@Override
	public String getYourScoreText()
	{
		return Global.app.getString(R.string.your_score);
	}

	@Override
	public void hideHelp()
	{
		isHelpHidden = true;
		Global.app.prefs.putObject(Prefs.CHALLENGE_HELP_KEY, true);
	}

	@Override
	public boolean isTimeConstraint()
	{
		return false;
	}

	@Override
	public boolean mustWinToUpdate()
	{
		return false;
	}

	@Override
	public void onGameOver(final GameActivity activity, final boolean isWinner)
	{
		challengeTime += GameSurface.level.getPlayingTime();

		final Record record = getRecord();
		final boolean isNext = isNextButton(isWinner);
		boolean isNewRecord = false;
		if (!isNext)
		{
			isNewRecord = RecordsManager.isNewRecordAndUpdate(record);
		}

		Dialogs.showGameDialog(activity, isWinner, isNext, record, isNewRecord);

		if (!isNext && isNewRecord)
		{
			// Check global record if challenge ended and there is local record.
			activity.runOnUiThread(new Runnable()
			{
				@Override
				public void run()
				{
					new IsWorldRecordTask(activity, record).execute();
				}
			});
		}
	}

	@Override
	public void onNext()
	{
		levelIndex++;
	}

	@Override
	public void showHelp(Activity activity)
	{
		if (isHelpNecessary(levelIndex))
		{
			Dialogs.showHelp(activity, recordId);
		}
	}

	@Override
	boolean isNextButton(boolean isWinner)
	{
		final int next = levelIndex + 1;
		final LevelInfo levelInfo = RecordsManager.getInfo(next);
		return isWinner && (next < Const.LEVELS) && !levelInfo.isLocked && levelInfo.isFree;
	}

	@Override
	protected boolean isHelpNecessary(int levelIndex)
	{
		return (levelIndex == 0) && !isHelpHidden;
	}
}
